package com.prj.ufdm.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.prj.ufdm.demo.model.mongo.Employee;
import com.prj.ufdm.demo.mongdao.EmployeeRepository;

@RestController
@RequestMapping("/mongo")
public class MongodAction {

   @Autowired
   private EmployeeRepository employeeRepository;

   @Autowired
   private MongoTemplate mongoTemplate;
   
   @RequestMapping("/test1")
   public void test1() {
       Employee employee = new Employee();
       employee.setName("王五");
       employee.setAge(29);
       employeeRepository.save(employee);
       System.out.println(employee);
   }

   @RequestMapping("/test2")
   public void test2() {
       Employee employee = employeeRepository.findByName("张三");
       System.out.println(employee);
   }

   @RequestMapping("/test3")
   public void test3() {
       // 通过MongoTemplate来查询数据
       Query query = new Query(Criteria.where("age").in(20, 23));
       List<Employee> employees = mongoTemplate.find(query, Employee.class);
       System.out.println(employees);
   }

   @RequestMapping("/test4")
   public void test4() {
       // 查询年龄大于25的数据
       List<Employee> employeeList = employeeRepository.findByAgeGreaterThan(25);
       System.out.println(employeeList);
   }
	    
}
